//
//  HWYouxiFenxiangGameView.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/11.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWYouxiFenxiangGameView.h"

@interface HWYouxiFenxiangGameView()
@property(strong,nonatomic) UIView *grayView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) NSArray *icoArr;
@property(strong,nonatomic) NSArray *listArr;

@end

@implementation HWYouxiFenxiangGameView

- (instancetype)initWithFrame:(CGRect)frame {
    
    
    
    if (self = [super initWithFrame:frame]) {
        
        // 添加灰色背景
        [self addgrayView];
        
          [self setupUI];
        
    }
    
    return  self;
    
    
}


//  设置UI
- (void)setupUI {
    
//    self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-120-38-200-TabbarSafeBottomMargin, kScreenW, 120+38+200+TabbarSafeBottomMargin)];
//    self.fenxiangView.backgroundColor = RGB(240, 244, 246);
//    [self addSubview:self.fenxiangView];
//
//
//    UILabel *fenxianlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 16, kScreenW, 13)];
//    fenxianlabel.text = @"分享游戏";
//    fenxianlabel.font = [UIFont systemFontOfSize:13];
//    fenxianlabel.textColor = RGB(51, 51, 51);
//    fenxianlabel.textAlignment =  NSTextAlignmentCenter;
//    [self.fenxiangView  addSubview:fenxianlabel];
//
//    self.listArr = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
//    self.icoArr = @[@"share_w",@"share_q",@"share_qqico",@"share_c"];
//    CGFloat jianju = (kScreenW - 82-4*50)/3;
//    // CGFloat  shujianju = (166-100)/3;
//
//    for (int i = 0 ; i < self.listArr.count; i++) {
//
//
//        UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(jianju*i+41+i*50, 52, 50, 50) Title:self.listArr[i] Image:self.icoArr[i] Font:13 Color:RGB(51, 51, 51) Target:self Selector:@selector(buttonTap:)];
//        butt.tag = i;
//        [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
//        // butt.backgroundColor = [UIColor redColor];
//        [self.fenxiangView addSubview:butt];
//
//    }
//
//
//
//    UILabel *mianduimianLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 120, kScreenW, 49)];
//    mianduimianLabel.text = @"面对面邀请好友";
//    mianduimianLabel.textAlignment  = NSTextAlignmentCenter;
//    mianduimianLabel.textColor = RGB(51, 51, 51);
//    mianduimianLabel.font = [UIFont systemFontOfSize:14];
//    [self.fenxiangView addSubview:mianduimianLabel];
//
//
//
//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-110)/2, mianduimianLabel.bottom, 110, 110)];
//    imageView.image = [UIImage imageNamed:@"erweima"];
//    [self.fenxiangView addSubview:imageView];
//
//
//
//    UILabel *changankebaocun = [[UILabel alloc]initWithFrame:CGRectMake(0, imageView.bottom, kScreenW, 41)];
//    changankebaocun.text = @"长按即可保存";
//    changankebaocun.textAlignment  = NSTextAlignmentCenter;
//    changankebaocun.textColor = RGB(51, 51, 51);
//    changankebaocun.font = [UIFont systemFontOfSize:14];
//    [self.fenxiangView addSubview:changankebaocun];
//
//
//
//
//    UIView *fengeview = [[UIView alloc]initWithFrame:CGRectMake(0,120+200-1, kScreenW, 1)];
//    fengeview.backgroundColor = RGB(215, 215, 215);
//
//
//    [self.fenxiangView addSubview:fengeview];
//
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(0, 120+200, kScreenW, 38);
//    [button setTitle:@"取消" forState:UIControlStateNormal];
//    [button setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(quiabuttontap) forControlEvents:UIControlEventTouchUpInside];
//    button.titleLabel.font = [UIFont systemFontOfSize:14];
//    [self.fenxiangView addSubview:button];
//
//
//
//
    
    
    self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-38-200-TabbarSafeBottomMargin, kScreenW, 38+200+TabbarSafeBottomMargin)];
       self.fenxiangView.backgroundColor = RGB(240, 244, 246);
       [self addSubview:self.fenxiangView];
       
       
//       UILabel *fenxianlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 16, kScreenW, 13)];
//       fenxianlabel.text = @"分享游戏";
//       fenxianlabel.font = [UIFont systemFontOfSize:13];
//       fenxianlabel.textColor = RGB(51, 51, 51);
//       fenxianlabel.textAlignment =  NSTextAlignmentCenter;
//       [self.fenxiangView  addSubview:fenxianlabel];
//
//       self.listArr = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
//       self.icoArr = @[@"share_w",@"share_q",@"share_qqico",@"share_c"];
//       CGFloat jianju = (kScreenW - 82-4*50)/3;
//       // CGFloat  shujianju = (166-100)/3;
//
//       for (int i = 0 ; i < self.listArr.count; i++) {
//
//
//           UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(jianju*i+41+i*50, 52, 50, 50) Title:self.listArr[i] Image:self.icoArr[i] Font:13 Color:RGB(51, 51, 51) Target:self Selector:@selector(buttonTap:)];
//           butt.tag = i;
//           [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
//           // butt.backgroundColor = [UIColor redColor];
//           [self.fenxiangView addSubview:butt];
//
//       }
//
       
       
       UILabel *mianduimianLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 49)];
       mianduimianLabel.text = @"面对面邀请好友";
       mianduimianLabel.textAlignment  = NSTextAlignmentCenter;
       mianduimianLabel.textColor = RGB(51, 51, 51);
       mianduimianLabel.font = [UIFont systemFontOfSize:14];
       [self.fenxiangView addSubview:mianduimianLabel];
       
       
       
       UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-110)/2, mianduimianLabel.bottom, 110, 110)];
       imageView.image = [UIImage imageNamed:@"erweima"];
       self.qrcodeImageView = imageView;
       self.qrcodeImageView.userInteractionEnabled = YES;
       [self.fenxiangView addSubview:imageView];
//
//      UILongPressGestureRecognizer *longPressGr =          [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressToDo:)];
//      longPressGr.minimumPressDuration=3.0;
//
//      [ self.qrcodeImageView  addGestureRecognizer:longPressGr];
     
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(baocun)];
    [self.qrcodeImageView addGestureRecognizer:tap];
    
    
       
       
       UILabel *changankebaocun = [[UILabel alloc]initWithFrame:CGRectMake(0, imageView.bottom, kScreenW, 41)];
       changankebaocun.text = @"点击即可保存";
       changankebaocun.textAlignment  = NSTextAlignmentCenter;
       changankebaocun.textColor = RGB(51, 51, 51);
       changankebaocun.font = [UIFont systemFontOfSize:14];
       [self.fenxiangView addSubview:changankebaocun];
       
       
       
       
       UIView *fengeview = [[UIView alloc]initWithFrame:CGRectMake(0,0+200-1, kScreenW, 1)];
       fengeview.backgroundColor = RGB(215, 215, 215);


       [self.fenxiangView addSubview:fengeview];

       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
       button.frame = CGRectMake(0, 0+200, kScreenW, 38);
       [button setTitle:@"取消" forState:UIControlStateNormal];
       [button setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
       [button addTarget:self action:@selector(quiabuttontap) forControlEvents:UIControlEventTouchUpInside];
       button.titleLabel.font = [UIFont systemFontOfSize:14];
       [self.fenxiangView addSubview:button];

       
}



-(void)longPressToDo:(UILongPressGestureRecognizer*)gesture
 {
//直接return掉，不在开始的状态里面添加任何操作，则长按手势就会被少调用一次了
   if(gesture.state!=UIGestureRecognizerStateBegan)
   {
       
       
//       UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"图片" message:@"是否保存图片"
//                       preferredStyle:UIAlertControllerStyleAlert];
//
//         UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
//                                                               handler:^(UIAlertAction * action) {
//                                                                   //响应事件
//                                            UIImageWriteToSavedPhotosAlbum(self.qrcodeImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//                                                               }];
//         UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
//                                                               handler:^(UIAlertAction * action) {
//                                                                   //响应事件
//
//                                                               }];
//
//         [alert addAction:defaultAction];
//         [alert addAction:cancelAction];
//         [[HWViewUtils getCurrentVC] presentViewController:alert animated:YES completion:nil];
       
       
//       UIImageWriteToSavedPhotosAlbum(self.qrcodeImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
          #pragma mark -- <保存到相册>
      return;
   }

 }


- (void)relayoutTheChildControls {
//    // self.whiteView.backgroundColor = [UIColor redColor];
//    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 120);
//    self.grayView.frame = CGRectMake(0, self.whibackvieY+120, kScreenW, kScreenH-self.whibackvieY-118+10);
//
    
    
    
}

- (void)baocun {
    
     UIImageWriteToSavedPhotosAlbum(self.qrcodeImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}


 
   
    


-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
        [MBProgressHUD showSuccess:@"保存失败"];
        
    }else{
        msg = @"保存图片成功" ;
           [MBProgressHUD showSuccess:@"保存成功"];
    }
}






// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.6;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];
    

}

- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
}


//点击buton 按钮

- (void)buttonTap:(UIButton *)sender {
    
    [self.delegate xuazhongTag:(int)sender.tag];
    
    
    
   
    
}

#pragma mark ------ 取消
- (void)quiabuttontap {
    
    [self removeFromSuperview];
    
    
}



//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//
//        return NO;
//    }else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
//
//
//        return NO;
//    }
//    return YES;
//
//
//}



@end
